﻿#########################################################################################################
####	Author: Rahul G. Babar																		 ####
####    Purpose: Use this PowerShell script to uninstall and remove all the custom solutions from    ####
####             the SharePoint farm solution store.												 ####
####    Usage Example: .\CleanupFarmSolutionStore.ps1 -Confirm:$false                                ####
#########################################################################################################

[CmdletBinding()]
Param(
   [Parameter(Mandatory=$true,Position=1)]
   [bool]$Confirm
) 

function LoadSharePointPowerShellEnvironment
{	
	write-host "Setting up PowerShell environment for SharePoint" -foregroundcolor Yellow
	write-host 
	Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
	write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
	write-host 
}

LoadSharePointPowerShellEnvironment

$AdminServiceName = "SPAdminV4"  
$WasAdminServiceRunning = $true;  

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")   
{   
	write-host "[[STEP]] Starting SharePoint Administration Service since it is not already started." -foregroundcolor Yellow
	write-host

    $WasAdminServiceRunning = $false;   
    Start-Service $AdminServiceName    

	write-host "[[STEP]] SharePoint Administration Service Started" -foregroundcolor Green
	write-host
} 

Start-SPAssignment -Global;
$solutions = Get-SPSolution
foreach($solution in $solutions)
{
	if($solution -ne $null)
	{
		$solutionName = $solution.Name
		if($solution.Deployed)
		{
			Write-Host "Uninstalling solution $solutionName..." -ForegroundColor Yellow
			write-host 
			if($solution.DeployedWebApplications.Count -gt 0)
			{
				Uninstall-SPSolution -Identity $solution -AllWebApplications -Confirm:$Confirm
			}
			else
			{
				Uninstall-SPSolution -Identity $solution -Local:$true -Confirm:$Confirm
			}
			do 
			{
			  	Start-Sleep 5;
			  	$solution = Get-SPSolution $solution;
			} while($solution.JobExists -or $solution.Deployed)
			Write-Host "Unistalled solution $solutionName successfully." -ForegroundColor Green		
			write-host 
		}	 
	
		Write-Host "Removing solution $solutionName..." -ForegroundColor Yellow
		write-host
		Remove-SPSolution -Identity $solution -Confirm:$Confirm		
		do 
		{
		  	Start-Sleep 5;
		  	$solution = Get-SPSolution $solution -ErrorAction SilentlyContinue
		} while($solution -ne $null)
		Write-Host "Removed solution $solutionName successfully." -ForegroundColor Green
		write-host 
	}
}
Stop-SPAssignment -Global;

if (-not $WasAdminServiceRunning)   
{   
	write-host "[[STEP]] Stopping SharePoint Administration Service." -foregroundcolor Yellow
	write-host
    Stop-Service $AdminServiceName  
	write-host "[[STEP]] SharePoint Administration Service Stopped." -foregroundcolor Green
	write-host
} 

Write-Host "Farm solution store cleanup finished" -ForegroundColor Green